package com.example.springmybatis.control;

import com.example.springmybatis.entity.*;
import com.example.springmybatis.result.Result;
import com.example.springmybatis.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class request {
    @Resource
    private Adminserver adminserver;
    @Resource
    private Classservice classservice;
    @Resource
    private Communityservice communityservice;
    @Resource
    private Newsservice Newsservice;
    @Resource
    private Featureservice featureservice;

    @RequestMapping("/hello")
    public String hello(){
      return "字符串";
    };

//    焦点图接口
    @GetMapping("/banner")
    public Result classlist(){
        List<Classlist> classlists=classservice.getClasslist();
        System.out.println();
        return Result.success(classlists);
    }
//    社区分类制作
    @GetMapping("/community")
    public Result community(){
        List<Community> communityList=communityservice.getcommunity();
        return Result.success(communityList);
    }
    @GetMapping("/communitycategory")
    public Result communitycategory(){
        List<Communitycategory> communitycategoryList=communityservice.getcategory();
        return Result.success(communitycategoryList);
    }
//    新闻接口
    @GetMapping("/newslist")
    public Result newslist(){
        List<news> news=Newsservice.getnews();
        return Result.success(news);
    }
    @GetMapping("/newcate")
    public Result newcate(){
        List<news_categories> news=Newsservice.getcate();
        return Result.success(news);
    }
//  功能接口
    @GetMapping("/feature")
    public Result feature(){
        List<feature> features=featureservice.getfeature();
        return Result.success(features);
    }
    @GetMapping("/features")
    public Result features(){
        List<feature> features=featureservice.getfeatures();
        return Result.success(features);
    }
    @GetMapping("/videos")
    public Result videos(){
        List<Videos> videos=featureservice.getVideos();
        return Result.success(videos);
    }
    @GetMapping("/videoscategory")
    public Result videocategory(){
        List<Videoscategory> videocategory=featureservice.getCategory();
        return Result.success(videocategory);
    }


//    徽章接口
    @GetMapping("/medal")
//    视频接口
    public Result medal(){
        List<medal> medals=featureservice.getmedal();
        return Result.success(medals);
    }
//    登录接口制作
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        Admin admin1= adminserver.login(admin);

        if(admin1.getPassword().equals(admin.getPassword())){
            return Result.success(admin1);
        }else return Result.error("错误");
    }
    @PostMapping("/register")
    public Result register(@RequestBody Admin admin){
        Admin admin1= adminserver.register(admin);
        System.out.println("admin1 = " + admin1);
        if(admin1==null){
            return Result.success(admin1);
        }else return Result.error("错误");
    }
    @PostMapping("/imgUpDown")
    public Result imgUpDown(@RequestParam("file") MultipartFile file,String id) throws IOException {
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取文件后缀名。也可以在这里添加判断语句，规定特定格式的图片才能上传，否则拒绝保存。
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //为了避免发生图片替换，这里使用了文件名重新生成
        fileName = UUID.randomUUID()+suffixName;
        String path = "H:\\计算机学习\\graduate-dev\\graduate-springboot\\src\\main\\resources\\static\\admin_img\\";
        file.transferTo(new File(path+fileName));
        String res=adminserver.upimg(fileName,id);
        System.out.println("res = " + res);
        if (res==null){
            return Result.success();
        }else return Result.error("修改图片错误");

    }
}
